﻿using Api.Application.Command.Menu;
using Api.DTO;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Menu
{
    public class GetFarXiaHnadler : IRequestHandler<GetFarXiaCommand, ApiResult<List<MenuDTO>>>
    {
        private readonly IBase<MenuModel> menuRepo;

        public GetFarXiaHnadler(IBase<MenuModel> menuRepo)
        {
            this.menuRepo = menuRepo;
        }
        public Task<ApiResult<List<MenuDTO>>> Handle(GetFarXiaCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<MenuDTO>> result = new ApiResult<List<MenuDTO>>();
            var menu = menuRepo.GetAll().Where(x => x.PId == 0 && x.IsDeleted == false);
            var linq = from a in menu
                       select new MenuDTO()
                       {
                           PId = a.PId,
                           CrearName = a.CrearName,
                           CrearTime = a.CrearTime,
                           MenuName = a.MenuName,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                           Url = a.Url,
                           Id = a.Id,
                           ParentName = a.MenuName,
                       };
            result.Msg = "父级菜单显示成功";
            result.Data = linq.OrderByDescending(x => x.CrearTime).ToList();
            result.Code = ApiEnum.OK;
            return Task.FromResult(result);
        }
    }
}
